<?php


namespace App\Admin\Controllers\Imports;

use App\Imports\StaffImport;
use App\Models\Schedule;
use Carbon\Carbon;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;

/**
 * Created By FreeAbrams
 * Date: 2020/11/27
 */
class ScheduleImportController
{
	// 文件后缀
	protected $extensions = [
		'xls',
		'xlsx'
	];
	public function index(Request $request, Content $content)
	{
		$import_time = new Carbon($request->post('dateTime'));
		$import_time = $import_time->startOfMonth();
		// 查询到导入记录后渲染查询到的数据
		return $content
					->header('排班记录表导入')
					->row(view('admin.import.date-input', ['route' => route('schedule.import')]))
					->row(view('admin.import.upload', ['route' => route('schedule.import')]))
					->row($this->grid($import_time));
	}
	
	// 导入文件
	// 导入会覆盖之前的文件 原来的数据从磁盘上擦除然后重新导入
	public function Import(Request $request)
	{
		// 验证文件后缀
		$file = $request->file('file');
		$canUpload = in_array($file->extension(), $this->extensions);
		if(!$canUpload){
			return Response()->json([
				'code' => 0,
				'msg' => '不是合法的文件'
			]);
		}

		// 验证导入时间选择
		$validate = Validator::make($request->all(), [
			'import_time' => 'required'
		], ['import_time.required' => '排班统计时间未选择']);
		if($validate->fails()){
			return Response()->json([
				'code' => 0,
				'msg' => $validate->errors()->first()
			]);
		}
		
		// 导入会覆盖之前的文件 原来的数据从磁盘上擦除然后重新导入
		$import_time = new Carbon($request->post('import_time'));
		$old = Schedule::where('import_time', $import_time)->delete();
		$res = 0;
		$res = Excel::import(new StaffImport($import_time), $file);
		
		if($res){
			return Response()->json([
				'code' => 1,
				'msg' => '导入成功'
			]);
		} else {
			return Response()->json([
				'code' => 0,
				'msg' => '导入失败'
			]);
		}
	}
	
	protected function grid($import_time)
    {
        $grid = new Grid(new Schedule());
        $grid->model()->where('import_time', $import_time);
        
        $grid->column('staff_id', '员工编号')->editable();
        $grid->column('name', __('Name'))->editable();
        $grid->column('department', __('Department'));
        $grid->column('one', __('One'));
        $grid->column('two', __('Two'));
        $grid->column('three', __('Three'));
        $grid->column('four', __('Four'));
        $grid->column('five', __('Five'));
        $grid->column('six', __('Six'));
        $grid->column('seven', __('Seven'));
        $grid->column('eight', __('Eight'));
        $grid->column('nine', __('Nine'));
        $grid->column('ten', __('Ten'));
        $grid->column('eleven', __('Eleven'));
        $grid->column('twelve', __('Twelve'));
        $grid->column('thirteen', __('Thirteen'));
        $grid->column('fourteen', __('Fourteen'));
        $grid->column('fifteen', __('Fifteen'));
        $grid->column('sixteen', __('Sixteen'));
        $grid->column('seventeen', __('Seventeen'));
        $grid->column('eighteen', __('Eighteen'));
        $grid->column('nineteen', __('Nineteen'));
        $grid->column('twenty', __('Twenty'));
        $grid->column('twenty_one', __('Twenty one'));
        $grid->column('twenty_two', __('Twenty two'));
        $grid->column('twenty_three', __('Twenty three'));
        $grid->column('twenty_four', __('Twenty four'));
        $grid->column('twenty_five', __('Twenty five'));
        $grid->column('twenty_six', __('Twenty six'));
        $grid->column('twenty_seven', __('Twenty seven'));
        $grid->column('twenty_eight', __('Twenty eight'));
        $grid->column('twenty_nine', __('Twenty nine'));
        $grid->column('thirty', __('Thirty'));
        $grid->column('thirty_one', __('Thirty one'));
		
        $grid->filter(function (Grid\Filter $filter) {
			$filter->disableIdFilter();
			$filter->like('name', __('Name'));
		});
		$grid->tools(function (Grid\Tools $tools) {
			$tools->disableFilterButton();
			$tools->disableBatchActions();
		});
		$grid->disableColumnSelector();
		$grid->disableCreateButton();
		$grid->disableActions();
        
        return $grid;
    }
}